var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var app = getApp();

Page(common.extendPage(PageBase, {
    data: {
        dataLoaded: false,
        currentTab: 0,
        orderList: [],
        page: 1,
        deleting: false,
        shopName:''
    },

    onLoad() {
        this.setData({
            winHei: app.getWinHei()
        })
        this._toTab(0);
    },
    onShow() {},
    /**
     * 下拉加载更多
     */
    onReachBottom: function() {
        this._loadMore();
    },
    /**
     * 事件切换tab
     */
    _tabSwitch: function(evt) {
        var index = +evt.currentTarget.dataset.current || 0;
        this._toTab(index);
    },
    _toTab: function(index, cb) {
        this.setData({
            currentTab: index,
            page: 1,
            noMore: false,
        });
        this._getOrderList({ ty: index }, cb, true);
    },
    /**
     * 获取订单列表
     */
    _getOrderList: function(params, cb, isReset) {
        var ty = params.ty;
        var isMore = params.isMore;
        var page = this.data.page || 1;
        var url = '/market/Order/orderList';
        var data = {
            type: ty,
            page
        };

        if (isMore) {
            this.setData({
                moreLoading: true
            });
        } else {
            this.setData({
                loading: true
            });
        }
        ajax.G({
            url,
            data,
            success: (data) => {
                var list = data.result.order_list || [];
                var shopName = data.result.shop_title || '';

                var orderList = this.data.orderList;

                if (isReset) {
                    orderList = [];
                }
                var toData = {
                    orderList: orderList.concat(list),
                    noMore: list.length == 0,
                    page: this.data.page + 1,
                    dataLoaded: true,
                    shopName
                };
                toData[isMore ? 'moreLoading' : 'loading'] = false;
                this.setData(toData);
                cb && cb();
            }
        })
    },

    /**
     * 加载更多
     */
    _loadMore: function() {
        if (this.data.moreLoading || this.data.loading || this.data.noMore) {
            return;
        }
        this._getOrderList({
            ty: this.data.currentTab,
            isMore: true
        });
    },
    /**
     * 删除订单
     */
    _del: function(evt) {

        var url = '/market/Order/orderDel';
        var order_id = evt.currentTarget.dataset.order_id;
        var currentTab = this.data.currentTab;
        var data = {
            order_id
        }

        if (this.data.deleting) {
            util.alert('正在删除中，请稍后...');
            return false;
        }

        this.setData({
            deleting: true
        })

        ajax.G({
            url,
            data,
            success: (data) => {
                util.alert(data.msg, () => {
                    this._toTab(currentTab);
                })
                this.setData({
                    deleting: false
                })
            },
            fail: (data) => {
                this.setData({
                    deleting: false
                })
            }
        })
    }
}));